package tablization;

import java.util.Hashtable;
import java.util.Vector;

import utility.ConvertToLower;
import utility.Input;
import dbmanager.DBManager;
import file.SearchRecord;

/**
 *  @author Shweta
 **/
public class TablizationManager extends SubTable 
{
	
	String TABLEPATH =  Input.MASTERREPORTSPATH;
	String FILEFORMAT = Input.FILRFORMAT;
	
	Vector<Object> uniqueVector =new Vector<Object>();
	Vector<Object> multipleVector =new Vector<Object>();
	 
	
	public TablizationManager(DBManager sqlDB)
	{
		super.setDBManager(sqlDB);
	}
	
	
	/**
	 * this function returns set of objects of given type 
	 * @param typeName - name of type of whom objects is to get
	 * @return vector containing all objects of given type
	 */
	 private Vector<Object> getObjectSet(String typeName)
	 {	
	
			String pv = gin.getId(typeName);
		
		 Hashtable<Object,Object> arrayHash = new Hashtable<Object, Object>();				
			
			 arrayHash.put(PID_NAME, "type");
			 arrayHash.put(keyName, "pv");
			 arrayHash.put(valueName, pv);
				
			String [] strArray= getArray(arrayHash);
			
		 Vector< Object> vectorMid = new Vector<Object>();
		 
		 for(int i =0;i<strArray.length;i++)
			 	vectorMid.add(strArray[i].split("\t")[0]);	
	
		 return vectorMid;
	 }
		
	 
	/**
	 * this function gives list of properties with their property-objects respectively
	 * @param typeName - name of type of whom properties is to get
	 * @return array of string containing list of properties with their property-objects respectively
	 */
	 @SuppressWarnings("unchecked")
	private String[] getPropertyList(String typeName)
	{
			
		 String [] objArray ;
		 
		 String pidName = gin.getId("propertylist");
		 String midName = gin.getId(typeName);
		 String path = TABLEPATH + pidName + "//" + pidName + "_pid" + FILEFORMAT;
		 
		 Hashtable<Object,Object> tableHash = new Hashtable<Object, Object>();
		 tableHash.put("mid",midName);
		 
		 sr = new SearchRecord(path);
		 Hashtable<Object,Object> hasht = new Hashtable<Object, Object>();
		 hasht.putAll(sr.getRecordHash(tableHash));
		 
		 hasht.putAll(ConvertToLower.convertHashKey(hasht));
		 
		 String plObject = hasht.get("pv").toString();
		 path = TABLEPATH + plObject + "//" + plObject + "_mid" + FILEFORMAT;
		 
		 tableHash.put("mid",plObject);
		 
		 sr = new SearchRecord(path);
		 objArray = sr.getRecordSet(tableHash);
		
		
		 return objArray;
		 
	}
	
	
	/**
	 * this function gives set of properties on which table is to be created  
	 * @param objectArray - contains string array of properties with their property-objects respectively
	 * @return boolean value to set condition of having unique or multiple properties
	 */
	 @SuppressWarnings("unchecked")
	 private boolean getPropertyVector(String [] propertyObjectArray)
	 {
			
		 boolean flag = false; 
		 int size = propertyObjectArray.length;
		 String separator = Input.SEPARATOR;
		 
		 Hashtable<Object,Object> hasht;
		 
		 String [] pidArray = new String[size];
		 String [] pvArray = new String[size];
		 
		 String pidName = gin.getId("noOfOccurences");
		 String path = TABLEPATH + pidName + "//" + pidName + "_pid" + FILEFORMAT;
		 sr = new SearchRecord(path);
		 
		 for(int i=0; i<size; i++)
		 {
			 hasht = new Hashtable<Object, Object>();
			 
			 String [] tempArray = propertyObjectArray[i].split(separator);
			 pidArray[i] = tempArray[1];
			 pvArray[i] = tempArray[2];
		 
			 hasht.put("mid",pvArray[i]);		 
			 hasht.putAll(sr.getRecordHash(hasht));
			 hasht.putAll(ConvertToLower.convertHashKey(hasht));
			 
			 String pvName = hasht.get("pv").toString();
			 pvName = gin.getItem(pvName);
			 int pvId = Integer.parseInt(pvName);
			 
			 Hashtable< Object,Object> tempHash = new Hashtable<Object, Object>();
			 tempHash.put("pid", pidArray[i]);
			 if(pvId == 1)
			 {				
				 uniqueVector.add(tempHash);
			 }
			 else
			 {
				 multipleVector.add(tempHash);
				 flag = true;
			 }
		 }	
		 
		
		 return flag;
	 }	
	 
	 
	 /**
	  * this function checks for condition of creating table
	  * @param typeName - name of type of whom table is to be created
	  * @param noOfObjects - condition for creating table
	  * @return message as string
	  */
	 @SuppressWarnings("unchecked")
	public String createTablization(String typeName,String noOfObjects)
	 {
		
		 String message;
		 TableCreation tc;
		 int i = Integer.parseInt(noOfObjects);
		 
		 //set of objects ids for which table is to be created
		 Vector<Object> midVector = getObjectSet(typeName);
			
		 //condition to create table
			if(midVector.size() > i)
			{
				//list of properties with their respective property-objects
				String [] objectArray = getPropertyList(typeName);
				
				//set of properties on which table is to be created  
				boolean flag=getPropertyVector(objectArray);		
				
				tc = new TableCreation(typeName,midVector,sqlDB);
				
				//for unique properties boolean value will always be false
				if(!uniqueVector.isEmpty())
					tc.createTable(uniqueVector,false);
				
				if(!multipleVector.isEmpty())
				{
					for(int j=0;j<multipleVector.size();j++)
					{
						Hashtable<Object,Object> hasht = (Hashtable<Object, Object>)multipleVector.get(j);
						Vector<Object> tempVector = new Vector<Object>();
						tempVector.add(hasht);
						tc.createTable(tempVector,flag);
					}
				}
				
				message = "Tablization Completed";
			}
			else
				message = "Tablization can't be done";
				
			System.out.println(message);
		
			return message;
			
	}
	
	
	
	 public static void main(String[] args) 
	 {
		DBManager sqldb = new DBManager();
		sqldb.getConnect();
		
		TablizationManager tm = new TablizationManager(sqldb);
		
		String noOfObjects = "3";
		String object = "human2";
		
		if(tm.checkTableExistence(object) == true)
			tm.createTablization(object,noOfObjects);
	 }
 
	 
	
}
